/*
	FileName : JT_C_Power_Track.c
	Description :
		Monitor the track of power.
	
	Created on 2018.10.17 by WFL.

	Revision History:

*/


/* Includes ------------------------------------------------------------------*/


#include "JT_C_Power_Track.h"

//---------------------
JTC_POWER_TRACK JTC_power_t;
//---------------------

uint8_t JTC_POWER_TRACK_init(JTC_POWER_TRACK *power_t)
{
	uint8_t i;
	power_t->states = POWER_STATES_STARTUP;
	power_t->statesChangeCnt = 0;
	power_t->powerDropFound = 0;
	power_t->staySameCounter = 0;
	//------------------------------------------------
	power_t->direction = POWER_DERECTION_STARTUP;
	power_t->dirChangeCnt = 0;
	power_t->dir_sum_cnt = 0;
	for(i = 0; i< POWER_DERECTION_CASE_LEN; i++)
	{
		power_t->dir_sum[i] = 0;
	}
	return 0;
}

void JTC_POWER_TRACK_dirChange(JTC_POWER_TRACK *power_t,uint8_t dir)
{
	if(power_t->currentVal > POWER_HIGH_LEVEL)
		return;
	
	if(power_t->direction != dir)
	{
		if(power_t->dirChangeCnt < 0xFF)
			power_t->dirChangeCnt++;
		power_t->direction = dir;
	}
	return;
}
uint8_t JTC_POWER_TRACK_handle(JTC_POWER_TRACK *power_t,uint16_t powVal)
{
	uint8_t i;
	
	power_t->currentVal = powVal;
	
	if(powVal > POWER_HIGH_LEVEL)
	{
		if(power_t->states == POWER_STATES_HIGH)
		{
			if(power_t->staySameCounter < 0xFFFFFFFF)
				power_t->staySameCounter++;
			
			if(power_t->staySameCounter > POWER_STABLE_DELAY)
			{
				if(power_t->statesChangeCnt >= 2)
					power_t->powerDropFound = 1;
			}
		}
		else
		{
			if(power_t->staySameCounter >= POWER_STABLE_DELAY)
			{
				if(power_t->statesChangeCnt < 0xFF)
					power_t->statesChangeCnt++;
			}
			power_t->staySameCounter = 1;
		}
		
		power_t->states = POWER_STATES_HIGH;
	}
	
	if(powVal < POWER_LOW_LEVEL)
	{
		if(power_t->states == POWER_STATES_LOW)
		{
			if(power_t->staySameCounter < 0xFFFFFFFF)
				power_t->staySameCounter++;
		}
		else
		{
			if(power_t->staySameCounter >= POWER_STABLE_DELAY)
			{
				if(power_t->statesChangeCnt < 0xFF)
					power_t->statesChangeCnt++;
			}
			power_t->staySameCounter = 1;
		}
		
		power_t->states = POWER_STATES_LOW;
	}
	//---------------------------------------------------------
	power_t->dir_sum[0] += powVal;
	power_t->dir_sum_cnt++;
	if(power_t->dir_sum_cnt >= POWER_DERECTION_SECTION_LEN)
	{
		for(i = 0;i< POWER_DERECTION_CASE_LEN-1;i++)
		{
			if(power_t->dir_sum[i] >= power_t->dir_sum[i+1])
			{
				if(i != 0)
				{
					//JTC_POWER_TRACK_dirChange(power_t,POWER_DERECTION_POPPLE);
				}
				break;
			}
			if(i == POWER_DERECTION_CASE_LEN-2)
			{
				JTC_POWER_TRACK_dirChange(power_t,POWER_DERECTION_FALL);
			}
		}
		for(i = 0;i< POWER_DERECTION_CASE_LEN-1;i++)
		{
			if(power_t->dir_sum[i] <= power_t->dir_sum[i+1])
			{
				if(i != 0)
				{
					//JTC_POWER_TRACK_dirChange(power_t,POWER_DERECTION_POPPLE);
				}
				break;
			}
			if(i == POWER_DERECTION_CASE_LEN-2)
			{
				JTC_POWER_TRACK_dirChange(power_t,POWER_DERECTION_RISE);
			}
		}
		//--------------------------------------------------
		for(i = 0;i < POWER_DERECTION_CASE_LEN-1;i++)
		{
			power_t->dir_sum[POWER_DERECTION_CASE_LEN - 1 - i] = power_t->dir_sum[POWER_DERECTION_CASE_LEN - 2 - i];
		}
		power_t->dir_sum[0] = 0;
		power_t->dir_sum_cnt = 0;
	}
	
	return 1;
}

uint8_t JTC_POWER_TRACK_has_drop(JTC_POWER_TRACK *power_t)
{
//	if(power_t->powerDropFound)
//		return 1;
	if(power_t->direction == POWER_DERECTION_FALL || power_t->dirChangeCnt >= 2)
		return 2;
	return 0;
}
//-----------------------------------------------------------------------
///EOF
